/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utils;

import java.util.Date;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.jdesktop.swingx.JXDatePicker;

/**
 *
 * @author Kermes
 */
public class Validations {

    public static void setCor(JComponent c) {
        c.setBackground(Params.COR_CAMPO_VAZIO);
    }  

    public static void setFoco(JComponent c, boolean opc) {
        if (opc) {
            c.requestFocus();
        }
    }

    public static boolean textField_PhoneInvalid(JTextField c, boolean opc) {
        boolean isTrue = false;
        if (!c.getText().matches("\\(\\d{2}\\)\\d{4}\\-\\d{4}")) {
            setCor(c);
            setFoco(c, opc);
            isTrue = true;
        }
        return isTrue;
    }

    public static boolean textField_EmailInvalid(JTextField c, boolean opc) {
        boolean isTrue = true;
        if (!Utils.isEmail(c.getText().trim())) {
            setCor(c);
            setFoco(c, opc);
            isTrue = true;
        }
        return isTrue;
    }
    
    public static boolean textArea_IsEmpty(JTextArea c, boolean opc) {
        boolean isTrue = false;
        if (c.getText().trim().isEmpty()) {
            setCor(c);
            setFoco(c, opc);
            isTrue = true;
        }
        return isTrue;
    }

    public static boolean textField_IsEmpty(JTextField c, boolean opc) {
        boolean isTrue = false;
        if (c.getText().trim().isEmpty()) {
            setCor(c);
            setFoco(c, opc);
            isTrue = true;
        }
        return isTrue;
    }

    public static boolean datePiker_IsEmpty(JXDatePicker c, boolean opc) {
        boolean isTrue = false;
        if (c.getEditor().getText().trim().isEmpty()) {
            setCor(c.getEditor());
            setFoco(c, opc);
            isTrue = true;
        }
        return isTrue;
    }

    public static boolean datePiker_CheckDates(JXDatePicker home, JXDatePicker end, boolean opc) {
        boolean isTrue = false;
        Date dataF = end.getDate();
        Date dataI = home.getDate();
        if (dataF.before(dataI)) {
            setCor(end.getEditor());
            setFoco(end, opc);
            isTrue = true;
        }
        return isTrue;
    }

    public static boolean comboBox_InvalidSelection(JComboBox c, boolean opc) {
        boolean isTrue = false;
        if (c.getSelectedIndex() <= 0) {
            setCor(c);
            setFoco(c, opc);
            isTrue = true;
        }
        return isTrue;
    }

    public static boolean textField_NumberInvalid(JTextField c, boolean opc) {
        boolean isTrue = false;
        if (!c.getText().trim().isEmpty()) {
            try {
                int a = Integer.parseInt(c.getText().trim());
            } catch (NumberFormatException e) {
                setCor(c);
                setFoco(c, opc);
                isTrue = true;
            }
        }
        return isTrue;
    }

    public static boolean textField_NumberGreaterThan(JTextField c, boolean opc, int n) {
        boolean isTrue = false;
        if (!c.getText().trim().isEmpty()) {
            if (Integer.parseInt(c.getText().trim()) > n) {
                setCor(c);
                setFoco(c, opc);
                isTrue = true;
            }
        }
        return isTrue;
    }

    public static boolean textField_NumberLessThan(JTextField c, boolean opc, int n) {
        boolean isTrue = false;
        if (!c.getText().trim().isEmpty()) {
            if (Integer.parseInt(c.getText().trim()) < n) {
                setCor(c);
                setFoco(c, opc);
                isTrue = true;
            }
        }
        return isTrue;
    }

    public static boolean textField_InvalidSize(JTextField c, boolean opc, int MiniSize) {
        boolean isTrue = false;
        if (c.getText().trim().length() < MiniSize) {
            setCor(c);
            setFoco(c, opc);
            isTrue = true;
        }
        return isTrue;
    }

    public static boolean textArea_InvalidSize(JTextArea c, boolean opc, int MiniSize) {
        boolean isTrue = false;
        if (c.getText().trim().length() < MiniSize) {
            setCor(c);
            setFoco(c, opc);
            isTrue = true;
        }
        return isTrue;
    }

    public static boolean textField_CepIsEmpty(JTextField c, boolean opc) {
        boolean isTrue = false;
        if (c.getText().equalsIgnoreCase("  .   -   ")) {
            setCor(c);
            setFoco(c, opc);
            isTrue = true;
        }
        return isTrue;
    }

    public static boolean textField_CpfIsEmpty(JTextField c, boolean opc) {
        boolean isTrue = false;
        if (c.getText().equalsIgnoreCase("   .   .   -  ")) {
            setCor(c);
            setFoco(c, opc);
            isTrue = true;
        }
        return isTrue;
    }

    public static boolean textField_CnpjInvalid(JTextField c, boolean opc) {
        boolean isTrue = false;
        if (!Utils.isCNPJ(c.getText().trim())) {
            setCor(c);
            setFoco(c, opc);
            isTrue = true;
        }
        return isTrue;
    }

    public static boolean textField_CpfInvalid(JTextField c, boolean opc) {
        boolean isTrue = false;
        if (!Utils.isCPF(c.getText().trim())) {
            setCor(c);
            setFoco(c, opc);
            isTrue = true;
        }
        return isTrue;
    }

    public static boolean textField_CnpjIsEmpty(JTextField c, boolean opc) {
        boolean isTrue = false;
        if (c.getText().equalsIgnoreCase("  .   .   /    -  ")) {
            setCor(c);
            setFoco(c, opc);
            isTrue = true;
        }
        return isTrue;
    }

    public static boolean textField_InvalidCharacters(JTextField c, boolean opc) {
        boolean isTrue = false;
        String regex = "[a-zA-Z0-9°çÇãàâáÃÂÁÀéèêÉÈÊíìîÍÌÎóõôòÓÕÔÒùûúÙÛÚ., \\-\\/]+";
        if (!c.getText().trim().matches(regex)) {
            setCor(c);
            setFoco(c, opc);
            isTrue = true;
        }
        return isTrue;
    }
    
     public static boolean textArea_InvalidCharacters(JTextArea c, boolean opc) {
        boolean isTrue = false;
        String regex = "[a-zA-Z0-9°çÇãàâáÃÂÁÀéèêÉÈÊíìîÍÌÎóõôòÓÕÔÒùûúÙÛÚ., \\-\\/]+";
        if (!c.getText().trim().matches(regex)) {
            setCor(c);
            setFoco(c, opc);
            isTrue = true;
        }
        return isTrue;
    }

    public static boolean textField_InvalidCharactersContact(JTextField c, boolean opc) {
        boolean isTrue = false;
        String regex = "[\\\\d\\\\w@\\\\-\\\\.]";
        if (!c.getText().trim().matches(regex)) {
            setCor(c);
            setFoco(c, opc);
            isTrue = true;
        }
        return isTrue;
    }
}
